<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <meta name="author" content="隐星魂 (Roy.Sun)">
        
        <link rel="shortcut icon" href="../../img/favicon.ico">
        <title>08.软件定时器 - 玄武操作系统</title>
        <link href="../../css/bootstrap.min.css" rel="stylesheet">
        <link href="../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.3/styles/color-brewer.min.css">

        <script src="../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../js/bootstrap.min.js" defer></script>
        <script src="https://cdn.staticfile.org/highlight.js/10.0.3/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="../..">玄武操作系统</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="navitem">
                                <a href="../.." class="nav-link">主页</a>
                            </li>
                            <li class="navitem">
                                <a href="../../license/" class="nav-link">开源协议</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">快速开发指南 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">STM32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../QuickGuide/stm32/01-Envsetup-Guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/stm32/02-Project-Setup-Guide/" class="dropdown-item">工程配置指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/stm32/03-atkh743/" class="dropdown-item">正点原子阿波罗H743开发板上手指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/stm32/04-fk429m/" class="dropdown-item">反客F429核心板上手指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/stm32/05-atkf407core/" class="dropdown-item">正点原子F407核心板上手指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/stm32/06-atkf103core/" class="dropdown-item">正点原子F103核心板上手指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">NXP S32K</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../QuickGuide/s32k/01-Envsetup-Guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../QuickGuide/s32k/02-Project-Setup-Guide/" class="dropdown-item">工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">玄武内核模块</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../QuickGuide/xwko/01-XuanWuKO-Guide/" class="dropdown-item">上手指南</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">应用手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../00-index/" class="dropdown-item">00.目录</a>
</li>
                                    
<li>
    <a href="../01-brief/" class="dropdown-item">01.简介</a>
</li>
                                    
<li>
    <a href="../02-Basic-System-Architecture/" class="dropdown-item">02.系统基本架构</a>
</li>
                                    
<li>
    <a href="../03-Build-System/" class="dropdown-item">03.构建系统</a>
</li>
                                    
<li>
    <a href="../04-Boot-Flow/" class="dropdown-item">04.启动流程</a>
</li>
                                    
<li>
    <a href="../05-IRQ/" class="dropdown-item">05.中断控制</a>
</li>
                                    
<li>
    <a href="../06-SKD/" class="dropdown-item">06.调度器</a>
</li>
                                    
<li>
    <a href="../07-Thread/" class="dropdown-item">07.线程</a>
</li>
                                    
<li>
    <a href="./" class="dropdown-item active">08.软件定时器</a>
</li>
                                    
<li>
    <a href="../09-PM/" class="dropdown-item">09.电源管理</a>
</li>
                                    
<li>
    <a href="../10-Lock/" class="dropdown-item">10.锁</a>
</li>
                                    
<li>
    <a href="../11-Sync/" class="dropdown-item">11.同步</a>
</li>
                                    
<li>
    <a href="../12-MM/" class="dropdown-item">12.内存管理</a>
</li>
                                    
<li>
    <a href="../13-C-Lib/" class="dropdown-item">13.玄武C库</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">移植笔记 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../PortingNote/00-index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../PortingNote/01-Porting-Note/" class="dropdown-item">移植笔记</a>
</li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">ARM-Cortex-M</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../PortingNote/ARM-Cortex-M/01-ARM-Cortex-M-Porting-Note/" class="dropdown-item">架构移植笔记</a>
</li>
            
<li>
    <a href="../../PortingNote/ARM-Cortex-M/02-STM32-Porting-Note/" class="dropdown-item">STM32</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">规范 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../Specification/00-index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../Specification/01-Git-Commit-Specification/" class="dropdown-item">git-commit规范</a>
</li>
                                    
<li>
    <a href="../../Specification/02-Version-Branch-Specification/" class="dropdown-item">版本分支规范</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ml-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> 搜索
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../07-Thread/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> 上一篇
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../09-PM/" class="nav-link">
                                    下一篇 <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-secondary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-level="1"><a href="#8" class="nav-link">8.软件定时器</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#81" class="nav-link">8.1.描述</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#82" class="nav-link">8.2.用法</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#83" class="nav-link">8.3.示例</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#84" class="nav-link">8.4.配置</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#85api" class="nav-link">8.5.API参考</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#86" class="nav-link">8.6.附录</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="8">8.软件定时器</h1>
<h2 id="81">8.1.描述</h2>
<p>软件定时器是基于调度器的<a href="../06-SKD/#SKD-HWT-TASK">滴答定时器任务</a>实现的，
因此时间精度最小为滴答定时器的中断频率（通常配置为1000Hz）。
软件定时器的回调函数运行在滴答定时器任务中。</p>
<ul>
<li>当开启中断底半部时，软件定时器的回调函数运行在中断底半部中；</li>
<li>当关闭中断底半部时，软件定时器的回调函数运行在中断上下文中。</li>
</ul>
<p>无论是运行在中断底半部还是中断上下文，软件定时器的回调函数都
<strong>不</strong>可使用任何会导致睡眠、阻塞的API。</p>
<h2 id="82">8.2.用法</h2>
<h3 id="821">8.2.1.软件定时器的创建、初始化与删除、销毁</h3>
<p>软件定时器同线程一样，支持静态初始化与销毁，动态创建与删除两种方式：</p>
<ul>
<li>静态初始化与销毁<ul>
<li><strong>静态</strong>是指用户预先定义对象，这些对象在编译时由编译器分配内存。</li>
<li>初始化：<code>xwos_swt_init()</code></li>
<li>销毁：<code>xwos_swt_destroy()</code></li>
</ul>
</li>
<li>动态创建与删除<ul>
<li><strong>动态</strong>是指程序在运行时，通过内存分配函数从某个内存区域上申请分配一块内存，
    并把这块内存初始化为所需要的对象。使用完毕后，需要释放内存。</li>
<li>创建：<code>xwos_swt_create()</code></li>
<li>删除：<code>xwos_swt_delete()</code></li>
</ul>
</li>
</ul>
<h3 id="822">8.2.2.软件定时器的标志</h3>
<ul>
<li><code>XWOS_SWT_FLAG_RESTART</code></li>
</ul>
<p>定时器超时后自动重新开始（用于周期执行某项任务），</p>
<ul>
<li><code>XWOS_SWT_FLAG_AUTORM</code></li>
</ul>
<p>定时器超时后自动回收资源</p>
<ul>
<li><code>XWOS_SWT_FLAG_NULL</code></li>
</ul>
<p>无任何标志</p>
<blockquote>
<p><strong>注意</strong></p>
<ul>
<li><code>XWOS_SWT_FLAG_RESTART</code>不可与<code>XWOS_SWT_FLAG_AUTORM</code>同时设置，
    若同时设置，系统只会保留<code>XWOS_SWT_FLAG_RESTART</code>，另一标志被丢弃。</li>
<li><code>XWOS_SWT_FLAG_AUTORM</code>只对使用<code>xwos_swt_create()</code>创建的定时器有效。</li>
</ul>
</blockquote>
<h2 id="83">8.3.示例</h2>
<ul>
<li>应用模块：<code>xwam/example/timer</code></li>
<li>用法：<ul>
<li>在配置文件<code>xwbd/电路板名称/cfg/xwam.h</code>中
    定义宏<code>XWAMCFG_example_timer</code>为<code>1</code>；</li>
<li>在初始化流程中（例如：<code>xwos_main()</code>）调用<code>example_timer_start()</code>启动模块。</li>
</ul>
</li>
</ul>
<h2 id="84">8.4.配置</h2>
<h5 id="cfgxwosh">cfg/xwos.h</h5>
<ul>
<li>MP内核配置：<ul>
<li>调度器<ul>
<li><code>XWMPCFG_SYSHWT_PERIOD</code>：硬件定时器周期；</li>
<li><code>XWMPCFG_SKD_BH</code>：是否启用中断底半部，取值：1|0；</li>
<li><code>XWMPCFG_SKD_BH_STACK_SIZE</code>：中断底半部栈大小；</li>
<li><code>XWMPCFG_SKD_SWT</code>：是否启用软件定时器，取值：1|0；</li>
<li><code>XWMPCFG_SKD_SWT_MEMSLICE</code>：是否启用软件定时器对象缓存，取值：1|0；</li>
</ul>
</li>
</ul>
</li>
<li>UP内核配置：<ul>
<li>调度器<ul>
<li><code>XWUPCFG_SYSHWT_PERIOD</code>：硬件定时器周期；</li>
<li><code>XWUPCFG_SKD_BH</code>：是否启用中断底半部，取值：1|0；</li>
<li><code>XWUPCFG_SKD_BH_STACK_SIZE</code>：中断底半部栈大小；</li>
<li><code>XWUPCFG_SKD_SWT</code>：是否启用软件定时器，取值：1|0；</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="85api">8.5.API参考</h2>
<ul>
<li>头文件：<code>xwos/osal/swt.h</code></li>
<li>注释：见头文件</li>
</ul>
<h2 id="86">8.6.附录</h2>
<ul>
<li><a href="../00-index/">返回目录</a></li>
</ul></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../../js/base.js" defer></script>
        <script src="../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">搜索</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <p>
                    在这里所有文档：
                </p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="搜索..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
